﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Eqwww.Domain.Models.SystemManage;
using Eqwww.Code;
using System.Text;

namespace Eqwww.TagHelpers
{
    public class TreeCollapseTagHelper : Microsoft.AspNetCore.Razor.TagHelpers.TagHelper
    {
        public IEnumerable<TreeCollapseModel> Items { get; set; }

        private int layer = 0;

        public bool? HideTopNode { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "ul";
            var sb = GetCollapse(Items);
            output.Content.SetHtmlContent(sb);

        }

        private string GetCollapse(IEnumerable<TreeCollapseModel> data)
        {
            var sb = new StringBuilder();

            if (HideTopNode == true)
                sb.Append(string.Empty);
            else
            {
                sb.Append("<li class=\"mdui-list-item mdui-ripple\">");
                
                sb.Append("<div data-id='' data-name='父节点' class=\"mdui-list-item-content\"><div class='mdui-float-left'>父节点</div><div class='mdui-float-right eq-choose mdui-m-r-2 mdui-text-color-theme'>选择</div></div>");
                sb.Append("<i class=\"mdui-icon mdui-text-color-grey-400 material-icons\">publish</i>");
                sb.Append("</li>");
            }
            sb.Append(GetChildren(data, null));
            return sb.ToString();
        }

        public string GetChildren(IEnumerable<TreeCollapseModel> data, string parentId)
        {
            var sb = new StringBuilder();

            var parentName = string.Empty;
            if (!string.IsNullOrEmpty(parentId))
            {
                var parent = data.FirstOrDefault(x => x.Id == parentId);
                if (parent != null)
                    parentName = parent.Text;
            }

            var children = data.Where(x => x.ParentId == parentId).OrderBy(x=>x.SortCode);
            foreach (var item in children)
            {
                if (data.Any(x => x.ParentId == item.Id))
                {
                    sb.Append("<li class=\"mdui-collapse-item \">");
                    sb.Append("<div class=\"mdui-collapse-item-header mdui-list-item mdui-ripple\">");
                    if (!string.IsNullOrEmpty(item.Avatar))
                    {
                        sb.Append("<div class='mdui-list-item-avatar'><img src='" + item.Avatar + "'/></div>");
                    }

                    sb.Append("<div data-id='"+item.Id+"' data-name='"+item.Text+"' data-parent-name='"+parentName+"' class=\"mdui-list-item-content mdui-clearfix\"><div class='mdui-float-left'>" + item.Text + "</div>"+(item.Disabled==true?string.Empty:"<div class='mdui-float-right eq-choose mdui-m-r-2 mdui-text-color-theme'>选择</div>")+"</div>");
                    
                    sb.Append("<i class=\"mdui-collapse-item-arrow mdui-icon material-icons\">keyboard_arrow_down</i>");

                    sb.Append("</div>");

                    var needMargin = false;
                    var parent = data.FirstOrDefault(x => x.Id == item.ParentId);
                    if (parent != null)
                    {

                        needMargin = true;

                    }

                    sb.Append("<ul class=\"mdui-collapse-item-body mdui-list\" " + (needMargin ? "style='margin-left:60px;'" : string.Empty) + " mdui-collapse>");

                    sb.Append(GetChildren(data, item.Id));

                    sb.Append("</ul>");

                    sb.Append("</li>");
                    sb.Append("<li class='mdui-divider mdui-m-y-0'></li>");
                }
                else
                {
                    sb.Append("<li class=\"mdui-list-item mdui-ripple\">");
                    if (!string.IsNullOrEmpty(item.Avatar))
                    {
                        sb.Append("<div class='mdui-list-item-avatar'><img src='" + item.Avatar + "'/></div>");
                    }
                    sb.Append("<div data-id='" + item.Id + "' data-name='" + item.Text + "' data-parent-name='" + parentName + "' class=\"mdui-list-item-content\"><div class='mdui-float-left'>" + item.Text + "</div>" + (item.Disabled == true ? string.Empty : "<div class='mdui-float-right eq-choose mdui-m-r-2 mdui-text-color-theme'>选择</div>") + "</div>");
                    sb.Append("<i class=\"mdui-icon mdui-text-color-grey-400 material-icons\">publish</i>");
                    sb.Append("</li>");

                }


            }
            layer++;

            return sb.ToString();
        }
    }
}
